const CONST = require('../configs/const');
const BulletManager = require('../managers/bulletManager');
const FlyManager = require('../managers/flyManager');
const EnemyManager = require('../managers/enemyManager');
const EffectManager = require('../managers/effectManager');
const GameDataManager = require('../managers/gameDataManager');
const SoundManager = require('../managers/soundManager');
const HttpManager = require('../managers/httpManager');

module.exports = class g {
    constructor(mainJs) {
        this.gameName = '大鱼';
        //
        this.CONST = CONST;
        this.mainJS = mainJs;
        //
        this.bulletManager = new BulletManager();
        this.flyManager = new FlyManager();
        this.enemyManager = new EnemyManager();
        this.effectManager = new EffectManager();
        this.gameDataManager = new GameDataManager();
        this.soundManager = new SoundManager();
        this.httpManager = new HttpManager();
        //
        this.layerGame = cc.find('Canvas/layerGame');
        //
        this.waitLoad = 0;
        //
        this.init();
    }

    addOneLoad() {
        this.waitLoad++;
    }

    oneLoadOver() {
        this.waitLoad--;
        if (this.waitLoad == 0) {
            this.emit('loadAllResEnd');
        }
    }

    init() {
        //开启物理系统
        cc.director.getPhysicsManager().enabled = true;
        // 打印信息
        // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
        //     cc.PhysicsManager.DrawBits.e_jointBit |
        //     cc.PhysicsManager.DrawBits.e_shapeBit
        // ;
        console.log('程序入口 初始化完毕');
    }

    emit(event, ...data) {
        let gCanvas = cc.find('Canvas')
        if (gCanvas) {
            gCanvas.emit(event, ...data);
        } else {
            console.error(new Error('发射事件出错'));
        }
    }

    on(event, func) {
        let gCanvas = cc.find('Canvas')
        if (gCanvas) {
            gCanvas.on(event, func);
        } else {
            console.error(new Error('绑定事件出错'));
        }
    }

    randId(len = 10) {
        let id = '';
        for (let i = 0; i < len; i++) {
            let pos = Math.floor(Math.random() * CONST.ID_CONST.length);
            id += CONST.ID_CONST[pos];
        }
        return id;
    }
};